from main import db
from sqlalchemy.exc import InvalidRequestError
from main.utils import JSONHelper
from datetime import datetime, date
from sqlalchemy import or_, and_, func
from main.models import Equipment, EquipmentUsed


def query_equipment_by_id(id):
    return Equipment.query.filter(Equipment.id == id).first()


def query_equipment_list():
    qs = Equipment.query.filter()
    return qs.order_by(Equipment.create_time.desc()).all()


def query_equipmentUsed_list(condition):
    qs = EquipmentUsed.query.filter()
    if 'subscribe_id' in condition:
        qs = qs.filter(EquipmentUsed.subscribe_id == condition['subscribe_id'])
    return qs.order_by(EquipmentUsed.create_time.desc()).all()


def query_equipmentUsed_by_equipmentId(equipmentId):
    return EquipmentUsed.query.filter(EquipmentUsed.equipment_id == equipmentId).first()


def query_equipmentUsed_by_id(id):
    return EquipmentUsed.query.filter(EquipmentUsed.id == id).first()
